using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;

namespace LetsGoToSchool
{
    /// <summary>
    /// Interaction logic for ProgressReport.xaml
    /// </summary>
    public partial class ProgressReport : SurfaceWindow
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ProgressReport()
        {
            InitializeComponent();
            

            // Add handlers for Application activation events
            AddActivationHandlers();

            
            ProgressDataNode n = Database.generateReport(Main.CurrentUser);
            n.printData();
            createReport(n);
            LoadPieChartData(n._TotalMemoryPlaytime.TotalMinutes,n._TotalMusicPlaytime.TotalMinutes,n._TotalDoodlePlaytime.TotalMinutes);
            LoadMemoryLineChartData(Database.getMemoryGraphData(0));
            LoadMusicLineCharData(Database.getMusicGraphData());
        }

        public static string getIconFileName()
        {
            return "Icon-ProgressReport.png";
        }

        public void createReport(ProgressDataNode node)
        {
            _loginName.Text = Main.CurrentUser;

            //_totalPlaytime.Text = node._TotalPlaytime.ToString().Substring(0,node._TotalPlaytime.ToString().IndexOf('.'));
            _totalPlaytime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node._TotalPlaytime.Hours, node._TotalPlaytime.Minutes, node._TotalPlaytime.Seconds);
            _totalMemoryPlaytime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node._TotalMemoryPlaytime.Hours, node._TotalMemoryPlaytime.Minutes, node._TotalMemoryPlaytime.Seconds);
            _totalMusicPlaytime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node._TotalMusicPlaytime.Hours, node._TotalMusicPlaytime.Minutes, node._TotalMusicPlaytime.Seconds);
            _totalDoodlePlaytime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node._TotalDoodlePlaytime.Hours, node._TotalDoodlePlaytime.Minutes, node._TotalDoodlePlaytime.Seconds);
            //_totalMemoryPlaytime.Text = node._TotalMemoryPlaytime.ToString().Substring(0, node._TotalMemoryPlaytime.ToString().IndexOf('.'));
            //_totalMusicPlaytime.Text = node._TotalMusicPlaytime.ToString().Substring(0,node._TotalMusicPlaytime.ToString().IndexOf('.'));
            //_totalDoodlePlaytime.Text = node._TotalDoodlePlaytime.ToString().Substring(0,node._TotalDoodlePlaytime.ToString().IndexOf('.'));

            _totalSessions.Text = node._TotalSessions.ToString();
            _totalMemorySessions.Text = node._TotalMemorySessions.ToString();
            _totalMusicSessions.Text = node._TotalMusicSessions.ToString();
            _totalDoodelSessions.Text = node._TotalDoodleSessions.ToString();

            e_fastestTime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node.E_FastestTime.Hours, node.E_FastestTime.Minutes, node.E_FastestTime.Seconds);
            m_fastestTime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node.M_FastestTime.Hours, node.M_FastestTime.Minutes, node.M_FastestTime.Seconds);
            h_fastestTime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node.H_FastestTime.Hours, node.H_FastestTime.Minutes, node.H_FastestTime.Seconds);

            e_averageTime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node.E_AverageTime.Hours, node.E_AverageTime.Minutes, node.E_AverageTime.Seconds);
            m_averageTime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node.M_AverageTime.Hours, node.M_AverageTime.Minutes, node.M_AverageTime.Seconds);
            h_averageTime.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", node.H_AverageTime.Hours, node.H_AverageTime.Minutes, node.H_AverageTime.Seconds);

            e_averageMoves.Text = Math.Round(node.E_AverageMoves,2).ToString();
            m_averageMoves.Text = Math.Round(node.M_AverageMoves,2).ToString();
            h_averageMoves.Text = Math.Round(node.H_AverageMoves,2).ToString();

            e_bestSong.Text = node.BestSong;

            e_favoriteSong.Text = node.FavoriteSong;

            e_bestAccuracy.Text = Math.Round(((node.BestAccuracy)*100.0),0).ToString()+"%";

            e_averageAccuracy.Text = Math.Round(((node.AverageAccuracy) * 100.0), 0).ToString() + "%";

            //LOAD GRAPH DATA
        }


        private void LoadPieChartData(double memMin, double musMin, double doodleMin)
        {

            ((PieSeries)_pieChart.Series[0]).ItemsSource =

                new KeyValuePair<string, int>[]{

            new KeyValuePair<string, int>("Memory", (int) (memMin*100.0)),

            new KeyValuePair<string, int>("Music", (int) (musMin*100.0)),

            new KeyValuePair<string, int>("Doodle", (int) (doodleMin*100.0)) };

        }

        private void LoadMemoryLineChartData(KeyValuePair<int,double>[] dataPointList)
        {

            ((LineSeries)_lineChartMemory.Series[0]).ItemsSource = dataPointList;

        }

        private void LoadMusicLineCharData(KeyValuePair<int, double>[] dataPointList)
        {
            ((LineSeries)_lineChartMusic.Series[0]).ItemsSource = dataPointList;
        }

        private void _quitButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }


        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        private void SurfaceRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            string difficulty = (sender as SurfaceRadioButton).Content.ToString();
            if (difficulty.Equals("Easy"))
            {
                LoadMemoryLineChartData(Database.getMemoryGraphData(0));
            }
            else if (difficulty.Equals("Medium"))
            {
                LoadMemoryLineChartData(Database.getMemoryGraphData(1));
            }
            else if (difficulty.Equals("Hard"))
            {
                LoadMemoryLineChartData(Database.getMemoryGraphData(2));
            }
        }
    }
}